package com.ly.order.service.impl;

import com.ly.order.mapper.OrderMapper;
import com.ly.order.mapper.TransactionLogMapper;
import com.ly.order.service.OrderService;
import com.domain.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private TransactionLogMapper transactionLogMapper;



    @Override
    public List<Order> queryLocalhost(Order order) {
        List<Order> list = orderMapper.list(order);
        //没引进hutool
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return list.stream()
                .filter(item -> Objects.equals(item.getId(), 1))
                .peek(item->{
                    if(Objects.equals(item.getDate(),sdf.format(new Date()))){
                        System.out.println("1232321");
                    }
                })
                .collect(Collectors.toList());
    }

    @Override
    public int insert(Order order) {
        return orderMapper.insert(order);
    }

    //执行本地事务时调用，将订单数据和事务日志写入本地数据库，由mq监听调用
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void createOrder(Order order, String transactionId,BiConsumer<Order,String> tran) {
        //1.创建订单
        orderMapper.insert(order);

        //2.
        //执行事务部分 业务+事务id相结合
        if(tran!=null){
            tran.accept(order,transactionId);
        }
    }

    @Override
    public Order oneById(String id) {
        return orderMapper.oneById(id);
    }


}
